#include "test26_StlConString.h"



void test26_StlConString::classMain()
{
    std::cout << "test26_StlConString" << "\n";
    this->testString();
    //this->appendDemo("a","b");

}


// append() - 字符串拼接
void test26_StlConString::testString()
{
    std::string str = "a1b2c3d4";
    // 安全拷贝字符串
    //      strcpy
    //      ctrncpy
    //      ctrcpy_s
    std::string s1 = str; // 拷贝构造实现安全复制
    // 字符串替换
    std::string tmp = "m5";
    std::string sa = str.replace(0,3,"jak");

    // 字符串交换
    tmp.swap(sa);

    // 字符串切分 = 按指定分隔符切分，末尾的\\也算一个元素，tikens中可能有空字符串
    std::string st = "foo\\bar\\baz\\qux\\";
    std::string delimiter = "\\";  // 指定分隔符

    std::vector<std::string> tokens = test26_StlConString::splitString(st, delimiter);

    // 字符串切分
    auto ss = str.find("c3");
    if (str.find("c3") != std::string::npos) { // 常量静态成员值，对于 size_t 类型的元素具有最高可能值

    }
}

// append() - 字符串拼接
void test26_StlConString::appendDemo(std::string str1, std::string str2)
{
    // Appends str2 in str1 
    str1.append(str2);
    std::cout << "Using append(): \n";
    std::cout << str1 << std::endl;
}


std::vector<std::string> test26_StlConString::splitString(const std::string& str, const std::string& delimiter) {
    std::vector<std::string> result;
    size_t pos = 0;
    size_t prevPos = 0;

    while ((pos = str.find(delimiter, prevPos)) != std::string::npos) {
        std::string token = str.substr(prevPos, pos - prevPos);
        result.push_back(token);
        prevPos = pos + delimiter.length();
    }

    std::string lastToken = str.substr(prevPos);
    result.push_back(lastToken);

    return result;
}



